package com.swjd.jingle.fragment;

import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;
import com.hxl.okhttp.response.JsonArrayResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.swjd.jingle.R;
import com.swjd.jingle.adapter.LvAdapter;
import com.swjd.jingle.pojo.Goods;
import com.swjd.jingle.util.Constant;
import com.swjd.jingle.util.DemoDataProvider;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class PocketFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout layoutRefresh;
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.lv)
    ListView lv;
    private LvAdapter lvAdapter;
    private List<Goods> goods = new ArrayList<>();
    private int num = 0;
    private List<String> concerns;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void initialize() {
        //下拉刷新动画
        RefreshHeader header = getRefreshHeader(DemoDataProvider.Item.TaurusHeader);
        layoutRefresh.setRefreshHeader(header);
        layoutRefresh.autoRefresh();
    }

    @Override
    public void initViews() {
        lvAdapter = new LvAdapter(activity, goods);
        lv.setAdapter(lvAdapter);
        try {
            concerns = app.getUserInfo().getConcerns();
            goods.clear();
            for (int i = 0; i < concerns.size(); i++) {
                selectGoods(concerns.get(i));
                num = i;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void selectGoods(String username) {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        app.getOkHttpUtils().post(Constant.BASE_URL + Constant.selectUserByGoods, params, new JsonArrayResponseHandler() {
            @Override
            public void onSuccess(int statusCode, JSONArray response) {
                try {
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject data = (JSONObject) response.get(i);
                        Goods good = new Gson().fromJson(data.toString(), Goods.class);
                        goods.add(good);
                    }
                    if (num == concerns.size() - 1) {
                        lvAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                if (statusCode == 200) {
                    XToast.error(activity, "查询不到商品数据!", Toast.LENGTH_SHORT).show();
                } else {
                    XToast.error(activity, "网络请求失败!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_pocket;
    }

    @Override
    @OnClick(R.id.img_search)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_search:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        SearchFragment searchFragment = new SearchFragment();
                        searchFragment.setType(Constant.USER_TYPE);
                        fragmentManager.beginTransaction().replace(R.id.layout_main, searchFragment)
                                .addToBackStack(null)
                                .commit();
                    }
                }, 500);
                break;
        }
    }
}
